package com.note.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseCC {
    public static String driver;
    public static String url;
    public static String username;
    public static String password;
    static {
        Properties properties = new Properties();
        //类加载器加载资源文件
        InputStream is = BaseCC.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    // 通用查询                       数据库连接         sql语句      占位数据
    public static ResultSet execute(Connection coon, String sql, Object[] params, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
        preparedStatement = getConnection().prepareStatement(sql);
        if(params!=null)
            for(int i=0; i< params.length; i++){
                preparedStatement.setObject(i+1, params[i]);
            }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    public static int execute(Connection conn, String sql, Object[] params, PreparedStatement preparedStatement) throws SQLException {
        preparedStatement = getConnection().prepareStatement(sql);
        for (int i=0; i< params.length; i++){
            preparedStatement.setObject(i+1, params[i]);
        }
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }
    public static boolean closeResource(Connection connection,PreparedStatement pstm,ResultSet rs){
        boolean flag = true;
        if(rs != null){
            try {
                rs.close();
                rs = null;//GC回收
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(pstm != null){
            try {
                pstm.close();
                pstm = null;//GC回收
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(connection != null){
            try {
                connection.close();
                connection = null;//GC回收
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }
}
